#include "String.hpp"


String::String(): std::string() {}

String::String(const String &str): std::string(str) {}

String::String(const std::string& str): std::string(str) {}

String::String(const char* str): std::string(str) {}

String::String(const char c, int n): std::string(c, n) {}

String String::format(const char* fmt, ...)
{
	va_list args;
	va_start(args, fmt);

	// 首先确定需要的缓冲区大小
	va_list args_copy;
	va_copy(args_copy, args);

#ifdef _WIN32
	int needed = _vscprintf(fmt, args_copy);
#else
	int needed = vsnprintf(nullptr, 0, fmt, args_copy);
#endif
	va_end(args_copy);

	if(needed < 0)
	{
		va_end(args);
		return {}; // 格式化错误
	}

	// 分配足够大小的缓冲区（加1用于空终止符）
	size_t buffer_size = needed + 1;
#ifdef _WIN32
	std::unique_ptr<char[]> buffer(new char[buffer_size]);
	int result = vsprintf_s(buffer.get(), buffer_size, fmt, args);
#else
	std::vector<char> buffer(buffer_size);
	int result = vsnprintf(buffer.data(), buffer_size, fmt, args);
#endif
	va_end(args);

	if(result < 0)
	{
		return {}; // 格式化错误
	}

	return std::move(std::string(
#ifdef _WIN32
	                     buffer.get()
#else
	                     buffer.data()
#endif
	                 ));
}

String String::subFront(const char delimiter, bool last) const
{
	if(empty())
	{
		return {};
	}
	size_t pos;
	if(last)
	{
		pos = find_last_of(delimiter);
	}
	else
	{
		pos = find_first_of(delimiter);
	}

	if(pos == std::string::npos)
	{
		return *this;
	}

	return substr(0, pos);
}

String String::subAfter(const char delimiter, bool last) const
{
	if(empty())
	{
		return {};
	}
	size_t pos;
	if(last)
	{
		pos = find_last_of(delimiter);
	}
	else
	{
		pos = find_first_of(delimiter);
	}

	if(pos == std::string::npos)
	{
		return *this;
	}

	return substr(pos + 1);
}

String String::lower() const
{
	String str;
	str.reserve(this->size());
	std::transform(this->begin(), this->end(),  std::back_inserter(str), ::tolower);
	return std::move(str);
}

String String::upper() const
{
	String str;
	str.reserve(this->size());
	std::transform(this->begin(), this->end(), std::back_inserter(str), ::toupper);
	return std::move(str);
}

int String::toInt(size_t * _Idx, int _Base) const
{
	return std::stoi(*this, _Idx, _Base);
}

long String::toLong(size_t * _Idx, int _Base) const
{
	return std::stol(*this, _Idx, _Base);
}

long long String::toLongLong(size_t * _Idx, int _Base) const
{
	return std::stoll(*this, _Idx, _Base);
}

double String::toDouble(size_t * _Idx) const
{
	return std::stod(*this, _Idx);
}

bool String::toBool() const
{
	String str = this->lower();
	return str == "true";
}

std::vector<String> String::split(const String &delimiter) const
{
	std::vector<String> tokens;
	std::regex reg(delimiter);
	std::sregex_token_iterator iter(this->begin(), this->end(), reg, -1);
	std::sregex_token_iterator end;
	while(iter != end)
	{
		tokens.push_back(String(*iter++));
	}
	return tokens;
}

bool String::operator==(const String &str)
{
	return static_cast<const std::string &>(*this) == static_cast<const std::string &>(str);
}

bool String::operator==(const std::string & str)
{
	return static_cast<const std::string &>(*this) == str;
}

bool String::operator==(const char * str)
{
	return str != nullptr && static_cast<const std::string &>(*this) == str;
}

bool String::operator!=(const String &str) const
{
	return !(*this == str);
}

bool String::operator!=(const std::string& str) const
{
	return !(*this == str);
}

bool String::operator!=(const char* str) const
{
	return !(*this == str);
}

String String::operator+(const String &str) const
{
	return String(static_cast<const std::string &>(*this) + static_cast<const std::string &>(str));
}

String String::operator+(const std::string & str)const
{
	return String(static_cast<const std::string &>(*this) + str);
}

String String::operator+(const char * str) const
{
	return str != nullptr ? String(static_cast<const std::string &>(*this) + str) : *this;
}

String &String::operator=(const String &str)
{
	if(this != &str)
	{
		static_cast<std::string &>(*this) = static_cast<const std::string &>(str);
	}
	return *this;
}

String &String::operator=(const std::string& str)
{
	static_cast<std::string &>(*this) = str;
	return *this;
}

String &String::operator=(const char* str)
{
	static_cast<std::string &>(*this) = str ? str : "";
	return *this;
}

String &String::operator+=(const String &str)
{
	static_cast<std::string &>(*this) += static_cast<const std::string &>(str);
	return *this;
}

String &String::operator+=(const std::string& str)
{
	static_cast<std::string &>(*this) += str;
	return *this;
}

String &String::operator+=(const char* str)
{
	if(str != nullptr)
	{
		static_cast<std::string &>(*this) += str;
	}
	return *this;
}

std::ostream &operator<<(std::ostream & os, const String &str)
{
	// 将String转换为std::string，复用其流运算符
	os << static_cast<const std::string &>(str);
	return os;
}

std::istream &operator>>(std::istream & is, String &str)
{
	// 将String转换为std::string，复用其流运算符
	is >> static_cast<std::string &>(str);
	return is;
}

std::istream &getline(std::istream & is, String &str)
{
	// 读取到换行符为止（保留空格），并将内容存入std::string
	std::string temp;
	std::getline(is, temp);
	// 将std::string赋值给String对象
	str = temp;
	return is;
}